package com.songshuai.webapi.baseapi.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshuai.webapi.baseapi.dto.crmbase.BaseApiParmDto;
import com.songshuai.webapi.baseapi.dto.crmbase.BaseApiQueryResponse;
import com.songshuai.webapi.baseapi.dto.crmbase.CampusListItemDto;
import com.songshuai.webapi.baseapi.dto.request.*;
import com.songshuai.webapi.baseapi.dto.response.CampusListResDto;
import com.songshuai.webapi.baseapi.dto.response.CampusResDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.model.YixueBaseCampus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.service.CampusSchoolService;
import com.songshuai.webapi.baseapi.service.CampusService;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("校区")
@Slf4j
@RestController
@RequestMapping("/api/campus/")
public class CampusController {


    @Autowired
    private CampusService campusService;

    @Autowired
    private CampusSchoolService campusSchoolService;


    @ApiOperation(value = "获取校区列表")
    @PostMapping("queryCampusList")
    public BaseApiQueryResponse<List<CampusListResDto>> queryCampusList(@RequestBody BaseParmDto<CampusListReqDto> parmDto){
        Page<List<CampusListResDto>> page = PageHelper.startPage(parmDto.getPageIndex(), parmDto.getPageSize());
        List<CampusListResDto> campusList = campusService.getCampusList(parmDto.getParam());
        return BaseApiQueryResponse.setResData(campusList,page.getTotal());
    }

    @ApiOperation(value = "获取校区列表")
    @GetMapping("queryCampusByName")
    public QueryResponse<List<CampusListItemDto>> queryCampusByName(@RequestParam String campusName,@RequestParam(required = false) Integer size) {
        if (campusName == null) {
            return QueryResponse.setResData(null, ResultStatus.FAILOfficial, "请传入校区名称");
        }
        if (campusName.trim().length() < 2) {
            return QueryResponse.setResData(null, ResultStatus.FAILOfficial, "校区名至少为2个字符以上");
        }
        List<CampusListItemDto> campusList = campusService.getCampusByName(campusName, size);
        return QueryResponse.setResData(campusList);
    }


    @ApiOperation(value = "新增校区")
    @PostMapping("addCampus")
    public QueryResponse addCampus(@RequestBody CampusAddReqDto parmDto){
        YixueBaseCampus entity = new YixueBaseCampus();
        try {
            PropertiesUtil.copyProperties(parmDto,entity);
            Boolean flag = campusService.insertSelective(entity, parmDto.getCrmIds());
            if(flag){
                return new QueryResponse();
            }
        } catch (Exception e) {
            log.error("addCampus",e);
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }



    @ApiOperation(value = "新增负责学校")
    @PostMapping("bindSchool")
    public QueryResponse bindSchool(@RequestBody CampusBindSchoolReqDto parmDto){
        campusSchoolService.addSchool(parmDto);
        return new QueryResponse();
    }

    @ApiOperation(value = "删除负责学校")
    @PostMapping("deleteSchool")
    public QueryResponse deleteSchool(@RequestBody CampusDeleteSchoolReqDto parmDto){
        campusSchoolService.deleteSchool(parmDto);
        return new QueryResponse();
    }

    @ApiOperation(value = "删除校区")
    @PostMapping("deleteCampus")
    public QueryResponse deleteCampus(@RequestParam("campusId") Integer campusId){
        campusService.deleteCampus(campusId);
        return new QueryResponse();
    }


    @ApiOperation(value = "修改开业状态")
    @PostMapping("updateOpenStatus")
    public QueryResponse updateOpenStatus(@RequestBody CampusUpdateOpenStatusReqDto parmDto){
        campusService.updateOpenStatus(parmDto);
        return new QueryResponse();
    }


    @ApiOperation(value = "获取校区信息（根据校区id）")
    @GetMapping("queryCampusById")
    public QueryResponse<CampusResDto> queryCampusById(@RequestParam("campusId") Integer campusId){
        return QueryResponse.setResData(campusService.queryCampusById(campusId));

    }


    @ApiOperation(value = "修改校区信息（根据校区id）")
    @PostMapping("updateCampusById")
    public QueryResponse updateCampusById(@RequestBody CampusUpdateReqDto parmDto){
        YixueBaseCampus entity = new YixueBaseCampus();
        try {
            BeanUtils.copyProperties(entity,parmDto);
            entity.setId(parmDto.getCampusId());
            campusService.updateCampusByid(entity,parmDto.getCrmIds());
            return new QueryResponse();
        } catch (Exception e) {
            log.error("updateCampusById",e);
        }
        return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }









}
